<template>
  <Modal v-model:visible="showModal" title="资金流概况" width="1200px">
    <div class="flex flex-col">
      <div class="flex flex-row items-center mb-1">
        <a-select
          size="small"
          v-model:value="searchParams.year"
          :options="utils.genSelectOptions('year')"
          @change="loadData"
        ></a-select>
      </div>
      <vxe-table
        size="small"
        :loading="true"
        v-bind="customTableStyle"
        :data="tableData"
        class="custom-vxe-table"
      >
        <vxe-column field="year" title="年份" width="80">
          <template #default="{ row }"> {{ row.year }}年 </template>
        </vxe-column>
        <vxe-column field="month" title="月份" width="80" :sortable="true">
          <template #default="{ row }"> {{ row.month }}月 </template>
        </vxe-column>
        <vxe-column
          field="capitalDepositTotal"
          title="资金存款总额(亿元)"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }"> {{ row.capitalDepositTotal }} </template>
        </vxe-column>

        <vxe-column
          field="capitalDepositTotalTb"
          title="资金存款总额同比增长"
          minWidth="80"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.capitalDepositTotalTb ? row.capitalDepositTotalTb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>

        <vxe-column
          field="capitalDepositTotalHb"
          title="资金存款总额环比增长"
          minWidth="80"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.capitalDepositTotalHb ? row.capitalDepositTotalHb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>
        <vxe-column
          field="capitalLoanTotal"
          title="资金贷款总额(亿元)"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }"> {{ row.capitalLoanTotal }} </template>
        </vxe-column>
        <vxe-column
          field="capitalLoanTotalTb"
          title="资金贷款总额同比增长"
          minWidth="80"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.capitalLoanTotalTb ? row.capitalLoanTotalTb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>
        <vxe-column
          field="capitalLoanTotalHb"
          title="资金贷款总额环比增长"
          minWidth="80"
          :sortable="true"
        >
          <template #default="{ row }">
            {{ row.capitalLoanTotalHb ? row.capitalLoanTotalHb.toFixed(2) + '%' : '/' }}
          </template>
        </vxe-column>
        <vxe-column field="depositAndThan" title="存贷比" minWidth="100" :sortable="true">
          <template #default="{ row }"> {{ row.depositAndThan }}% </template>
        </vxe-column>
        <vxe-column
          field="personalConsumptionLoan"
          title="个人消费贷款额(亿元)"
          minWidth="120"
          :sortable="true"
        >
          <template #default="{ row }"> {{ row.personalConsumptionLoan }} </template>
        </vxe-column>
        <vxe-column
          field="enterpriseLoanAmount"
          title="企业贷款额(亿元)"
          minWidth="100"
          :sortable="true"
        >
          <template #default="{ row }"> {{ row.enterpriseLoanAmount }} </template>
        </vxe-column>
      </vxe-table>
    </div>
  </Modal>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Modal from '@/components/modal.vue'
import { Apis } from '@/api'
import { message } from 'ant-design-vue'
import * as utils from '../../utils'

const isLoading = ref(false)

const showModal = ref(false)

const searchParams = ref({
  year: '',
})
const tableData = ref<any[]>([])

// 统一管理表格样式
const customTableStyle = {
  ...utils.customTableStyle,
  footerMethod: ({ columns, data }) => {
    const footerData = [
      columns.map((column, columnIndex) => {
        if (columnIndex === 0) {
          return '合计'
        }
        if (
          [
            'capitalDepositTotal',
            'capitalLoanTotal',
            'personalConsumptionLoan',
            'enterpriseLoanAmount',
          ].includes(column.field)
        ) {
          return (utils.sumNum(data, column.field) || '') + '亿元'
        }
        if (['depositAndThan'].includes(column.field)) {
          const loanTotal = utils.sumNum(data, 'capitalLoanTotal') || 0
          const depositTotal = utils.sumNum(data, 'capitalDepositTotal') || 0
          return depositTotal === 0 ? '0%' : ((loanTotal / depositTotal) * 100).toFixed(2) + '%'
        }

        return ''
      }),
    ]
    return footerData
  },
}

function loadData() {
  isLoading.value = true
  Apis.szbt.capitalFlowData
    .get_list({
      params: {
        year: searchParams.value.year,
        pageNum: 1,
        pageSize: 20,
      },
    })
    .then((res) => {
      tableData.value = (res.rows || [])
        .filter((item) => item.year == searchParams.value.year)
        .sort((a: any, b: any) => {
          // 先按年份倒序
          if (b.month !== a.month) {
            return a.month - b.month
          }
        })
        .map((item: any, index: number) => {
          item.index = index + 1
          return item
        })

      // // 计算货物周转量
      // const totalGoodsTurnoverVolume = tableData.value.reduce(
      //   (sum, item) => sum + (item.goodsTurnoverVolume || 0),
      //   0,
      // )
      // tableData.value = tableData.value.map((item) => {
      //   item.goodsTurnoverVolume = (
      //     (item.goodsTurnoverVolume / totalGoodsTurnoverVolume) *
      //     100
      //   ).toFixed(2)
      //   return item
      // })

      // // 计算货运量
      // const totalTheVolume = tableData.value.reduce((sum, item) => sum + (item.theVolume || 0), 0)
      // tableData.value = tableData.value.map((item) => {
      //   item.theVolume = ((item.theVolume / totalTheVolume) * 100).toFixed(2)
      //   return item
      // })

      // const totalPassengerCount = tableData.value.reduce((sum, item) => sum + (item.passengerVolume || 0), 0);
      // tableData.value = tableData.value.map(item => {
      //   const passengerCount = item.passengerVolume || 0;
      //   item.passengerCountPercentage = totalPassengerCount ? ((passengerCount / totalPassengerCount) * 100).toFixed(2) : 0;
      //   return item;
      // });
    })
    .catch((err) => {
      message.error('获取数据失败')
    })
    .finally(() => {
      isLoading.value = false
    })
}

defineExpose({
  setData: (data: any) => {
    const { params } = data
    searchParams.value = params
    // tableData.value = data;
  },
  open: () => {
    showModal.value = true
    loadData()
  },
})
</script>

<style lang="scss" src="@/styles/custom-vxe-table.scss"></style>
